/**
 *  @author   karka.w
 *
 * 2013-12-9 
 */
package damuzee.data;

import org.mybatis.sql.PackageUtil;
import org.mybatis.sql.annotation.BaseResultMap;

import javax.persistence.Column;
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @author karka.w
 *
 */
public class CreateData {

	public static void main(String[] args) {
		CreateData data = new CreateData();
		String s = data.crateData() ;
		System.out.println(s);
	}
	
	public String crateData(){
			List<String> list = PackageUtil.getClassName("com.damuzee", true);
			
			StringBuffer sb = new StringBuffer();
			
			for (String className : list) {
				Class<?> clazz = null;
				try {
					clazz = Class.forName(className);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
				
				if (!(clazz.isAnnotationPresent(BaseResultMap.class))) 
					continue;
				Table table = clazz.getAnnotation(Table.class);
				String tablename = table.name();
				sb.append("-----------"+tablename+"---------------------\\r\\n") ;
				sb.append("create table ").append(tablename).append("\\r\\n");
				
				Field[] fields = clazz.getDeclaredFields();
				int i = 0 ;
				for (Field field : fields) {
					boolean isColumn = field.isAnnotationPresent(Column.class);
					if (isColumn) {
						Column column = (Column) field.getAnnotation(Column.class);
						if(i>0){
							sb.append(" ,");
						}
						 sb.append("\\t\\t   ").append(column.name()).append("   ").append(column.columnDefinition());
						 
						 i++ ;
					}
				}
				sb.append("\\r\\n");
			}
			return sb.toString() ;
	}
}
